﻿using System.Linq;
using System.Web.Mvc;
using iss.sg4210.se18.team6s.Utilities;
using iss.sg4210.se18.team6s.jass.DAL;
using iss.sg4210.se18.team6s.jass.DAL.DataAccess.Interfaces;
using iss.sg4210.se18.team6s.jass.DAL.DataAccess.MetaData;
using iss.sg4210.se18.team6s.jass.DAL.DataAccess.MSSQL;
using System.Collections.Generic;
using iss.sg4210.se18.team6s.jass.BL;
using System;

namespace iss.sg4210.se18.team6s.Controllers
{
    [HandleError]
    public class SearchController : Controller
    {
        private const int defaultPageSize = 10;
        SearchFacade searchFacade = null;
        AdvertisementFacade advertisementFacade = null;

        public SearchController()
        {
            searchFacade = new SearchFacade();
            advertisementFacade = new AdvertisementFacade();
        }

        //
        // GET: /Search/SearchAll
        [HttpPost]
        public ActionResult Search(SearchModel searchModel, string searchButton)
        {
            if (ModelState.IsValid)
            {
                IQueryable<Advertisement> searchResults = null;
                if (searchButton.Equals(ViewConstants.BTN_DISPLAY_ALL))
                    searchResults = searchFacade.GetAllSearchResults();
                else if (searchButton.Equals(ViewConstants.BTN_SEARCH))
                    searchResults = searchFacade.GetSearchResults(searchModel);

                searchModel.Result = searchResults.ToPagedList(0, defaultPageSize);
                Session[ViewConstants.SESSION_VAR_SEARCH_RESULTS] = searchResults;
                return View(searchModel);
            }

            return View(searchModel);
        }

        [HttpPost]
        public ActionResult Page(int? page, SearchModel searchModel, string searchButton)
        {
            if (ModelState.IsValid)
            {
                IQueryable<Advertisement> searchResults = null;
                if (searchButton.Equals(ViewConstants.BTN_DISPLAY_ALL))
                    searchResults = searchFacade.GetAllSearchResults();
                else if (searchButton.Equals(ViewConstants.BTN_SEARCH))
                    searchResults = searchFacade.GetSearchResults(searchModel);

                searchModel.Result = searchResults.ToPagedList(0, defaultPageSize);
                Session[ViewConstants.SESSION_VAR_SEARCH_RESULTS] = searchResults;
                return View("Search", searchModel);
            }

            return View("Search", searchModel);
        }

        public ActionResult Page(int? page)
        {
            IQueryable<Advertisement> list = (IQueryable<Advertisement>)Session[ViewConstants.SESSION_VAR_SEARCH_RESULTS];
            SearchModel newModel = new SearchModel();
            int currentPageIndex = page.HasValue ? page.Value - 1 : 0;
            newModel.Result = list.ToPagedList(currentPageIndex, defaultPageSize);
            return View("Search", newModel);
        }

        //
        // GET: /Search/Search

        public ActionResult Search()
        {
            return View();
        }

        public ActionResult Details(int id)
        {
            Advertisement advertisement = advertisementFacade.GetAdvertisement(id);
            if (!Common.IsLoggedInUserAdmin() && !Common.IsLoggedInUserAdvertiser())
            {
                //add view log
                AdvertisementViewLog viewLog = new AdvertisementViewLog();
                viewLog.AdvertisementId = advertisement.Id;
                viewLog.UserId = Common.GetLoggedInUserId();
                viewLog.AdvertisementViewTimeStamp = DateTime.Now;

                advertisementFacade.AddAdvertisementViewLog(viewLog);
            }
            return View(advertisement);
        }
    }
}
